//#ifndef EIGEN_USE_MKL_ALL
//#define EIGEN_USE_MKL_ALL
//#endif
#include "data_collector.hpp"
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <ctime>
#include <random>
#include <numeric>

using std::ifstream;
using std::stringstream;
using std::string;


void DataCollector::readDataFromFile(const string& fileName)
{
	ifstream ifd(fileName, ifstream::in);
	string line;
	stringstream ss;
	int cols,rows;
	if (!ifd.is_open())
	{
		std::cerr << "Open file: " << fileName << " error" << std::endl;
		exit(1);
	}
	std::getline(ifd, line);
	ss << line;
	ss >> cols>>rows;
	ss.clear();
	dataList = new MatrixXd(rows, cols);
	int j=0;
	while (std::getline(ifd, line))
	{
		ss << line;
		for (int i = 0; i < rows; i++)
			ss >> (*dataList)(i, j);
		ss.clear();
		j++;
	}
	ifd.close();
}
void DataCollector::sortByCol(int row) const
{
	MatrixXd tmp = *dataList;
	VectorXi index = VectorXi::LinSpaced(tmp.cols(), 0, tmp.cols() - 1);
	std::sort(index.data(), index.data() + index.size(),
		ColComparator<MatrixXd>(*dataList, row));
#ifdef _DEBUG
	std::cout << tmp << std::endl;
#endif // _DEBUG

	for (int i = 0; i < index.size(); ++i)
		dataList->col(i) = tmp.col(index(i));
#ifdef _DEBUG
	std::cout << *dataList << std::endl;
#endif
}

void DataCollector::premuteByCol() const
{
	std::srand((unsigned)std::time(0));
	MatrixXd tmp = *dataList;
	VectorXi index = VectorXi::LinSpaced(tmp.cols(), 0, tmp.cols() - 1);
	std::random_shuffle(index.data(), index.data() + index.size());
	for (int i = 0; i < index.size(); ++i)
		dataList->col(i) = tmp.col(index(i));
}
